package com.neusoft.neuiotms.dcom.repository.hn.impl;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.enums.HnInspectionTaskItemStatus;
import com.neusoft.neuiotms.dcom.model.enums.HnInspectionTaskStatus;
import com.neusoft.neuiotms.dcom.model.hn.HnInspectionTask;
import com.neusoft.neuiotms.dcom.model.hn.QHnInspectionTask;
import com.neusoft.neuiotms.dcom.repository.hn.CustomizedHnInspectionTaskRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedHnInspectionTaskRepositoryImpl extends AbstractRepository
        implements CustomizedHnInspectionTaskRepository {

    @Override
    public List<HnInspectionTask> index(final Searchable searchable) {
        final QHnInspectionTask task = QHnInspectionTask.hnInspectionTask;
        final BooleanBuilder where = this.where(searchable);
        final JPAQuery<HnInspectionTask> query = this.query().selectFrom(task);
        query.where(where);
        return query.fetch();
    }

    @Override
    public long count(final Searchable searchable) {
        final QHnInspectionTask task = QHnInspectionTask.hnInspectionTask;
        final BooleanBuilder where = this.where(searchable);
        final JPAQuery<HnInspectionTask> query = this.query().selectFrom(task);
        query.where(where);
        return query.fetchCount();
    }

    @Override
    public Page<HnInspectionTask> search(final Pageable pageable, final Searchable searchable) {
        final QHnInspectionTask task = QHnInspectionTask.hnInspectionTask;
        final BooleanBuilder where = this.where(searchable);
        final JPAQuery<HnInspectionTask> query = this.query().selectFrom(task);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder where(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QHnInspectionTask task = QHnInspectionTask.hnInspectionTask;

        //时间检索
        if (StringUtils.isNotBlank(searchable.getStrValue("startTaskTimeBegin"))) {
            where.and(
                    super.afterOrEqualsDate(task.startDateTime, searchable, "startTaskTimeBegin"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("startTaskTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(task.startDateTime, searchable, "startTaskTimeEnd"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("endDateTimeBegin"))) {
            where.and(super.afterOrEqualsDate(task.endDateTime, searchable,
                    "endDateTimeBegin"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("endDateTimeEnd"))) {
            where.and(super.beforeOrEqualsDate(task.endDateTime, searchable,
                    "endDateTimeEnd"));
        }

        if (StringUtils.isNotBlank(searchable.getStrValue("institutionCode"))) {
            where.and(super.equalsStr(task.institutionCode, searchable, "institutionCode"));
        }
        if (StringUtils.isNotBlank(searchable.getStrValue("institutionName"))) {
            where.and(super.equalsStr(task.institutionName, searchable, "institutionName"));
        }
        final String[] codes = searchable.getStrArray("institutionCodeArray");
        if ((codes != null) && (codes.length != 0)) {
            where.and(super.inStrings(task.institutionCode, searchable, "institutionCodeArray"));
        }

        //状态检索
        final String[] statusValueArray = searchable.getStrArray("status");
        if ((statusValueArray != null) && (statusValueArray.length != 0)) {
            final List<HnInspectionTaskStatus> statusArray =
                    Stream.of(statusValueArray).collect(Collectors.toList()).stream()
                            .map(it -> HnInspectionTaskStatus.valueOf(it)).collect(Collectors.toList());
            where.and(task.status.in(statusArray));
        }

        //巡检结果状态检索
        final String[] itemStatusValueArray = searchable.getStrArray("itemStatus");
        if ((itemStatusValueArray != null) && (itemStatusValueArray.length != 0)) {
            final List<HnInspectionTaskItemStatus> statusArray =
                    Stream.of(itemStatusValueArray).collect(Collectors.toList()).stream()
                            .map(it -> HnInspectionTaskItemStatus.valueOf(it)).collect(Collectors.toList());
            where.and(task.itemStatus.in(statusArray));
        }

        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return HnInspectionTask.class;
    }
}
